﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Beetle.Web.Composite;
using System.Web.UI;
using System.ComponentModel;
using Beetle.Web.Extensions;

namespace Beetle.Web.UI.WebControls
{
    public class DropDownList : System.Web.UI.WebControls.DropDownList, IExtensibleObject<Beetle.Web.UI.WebControls.DropDownList>, IEditStatus
    {

        #region IEditStatus 成员

        [DefaultValue(true)]
        public bool IsEditMode
        {
            get
            {
                object o = ViewState["IsEditMode"];
                return o != null ? (bool)o : true;
            }

            set
            {
                ViewState["IsEditMode"] = value;
            }
        }

        private bool? _InternalEditMode = null;
        public bool? InternalEditMode
        {
            get
            {
                return this._InternalEditMode;
            }
        }

        public void SetInternalEditModeValue(bool? b)
        {
            this._InternalEditMode = b;
        }

        public bool CheckEditMode()
        {
            if (!this.IsEditMode)
            {
                return false;
            }

            return this.GetEditMode();
        }


        #endregion

        #region IExtensibleObject<DropDownList> 成员


        private ExtensionCollection<Beetle.Web.UI.WebControls.DropDownList> extensions = null;

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtensionCollection<Beetle.Web.UI.WebControls.DropDownList> Extensions
        {
            get
            {
                return extensions;
            }
        }

        public DropDownList()
        {
            this.extensions = new ExtensionCollection<Beetle.Web.UI.WebControls.DropDownList>(this);
        }

        #endregion

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override System.Web.UI.WebControls.ListItemCollection Items
        {
            get
            {
                return base.Items;
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            bool isEditMode = this.CheckEditMode();
            if (!isEditMode)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }
        }

        public override void DataBind()
        {
            try
            {
                base.DataBind();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            catch
            {
                throw;
            }
        }
    }
}
